
import oss2


class OssUtil:

    def __init__(self):
        # OSS
        self.AccessKeyId = 'LTAIzSQ4dlg4Gl1E'
        self.AccessKeySecret = 'pRriwyuaidN3a5fM6gNm5pkGj2pu2r'
        if run_server():
            self.Endpoint = 'oss-cn-shenzhen-internal.aliyuncs.com'  # 阿里云内网
        else:
            self.Endpoint = 'oss-cn-shenzhen.aliyuncs.com'  # 外网
        self.Bucket = 'releasesdk'

    def upload_file(self, folder_name, oss_file_name, local_file_name):
        auth = oss2.Auth(self.AccessKeyId, self.AccessKeySecret)
        bucket = oss2.Bucket(auth, self.Endpoint, self.Bucket)
        key_name = "packageApk/yi_yuan_overseas/%s/%s" % (folder_name, oss_file_name)
        result = bucket.put_object_from_file(key_name, local_file_name)
        return result


def run_server():
    """
    当前是否为打包服务器
    :return:
    """
    if str(get_ip()).startswith("192"):
        return False
    else:
        return True


def get_ip():
    import socket
    # 获取本机电脑名
    myname = socket.gethostname()
    # 获取本机ip
    myaddr = socket.gethostbyname(myname)
    return myaddr